<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>我的购物车</span>
        </div>
      </template>
      
      <el-table
        v-loading="loading"
        :data="cartList"
        style="width: 100%"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="商品图片" width="120">
          <template #default="scope">
            <el-image 
              style="width: 80px; height: 80px"
              :src="scope.row.productImage"
              fit="cover"
            />
          </template>
        </el-table-column>
        <el-table-column prop="productName" label="商品名称" />
        <el-table-column prop="price" label="单价" width="120">
          <template #default="scope">
            ¥{{ scope.row.price }}
          </template>
        </el-table-column>
        <el-table-column label="数量" width="150">
          <template #default="scope">
            <el-input-number 
              v-model="scope.row.quantity" 
              :min="1" 
              :max="scope.row.stock"
              @change="handleQuantityChange(scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="小计" width="120">
          <template #default="scope">
            ¥{{ (scope.row.price * scope.row.quantity).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button
              type="danger"
              link
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="cart-footer">
        <div class="left">
          <el-button type="primary" @click="handleContinueShopping">
            继续购物
          </el-button>
        </div>
        <div class="right">
          <span class="total-price">
            合计：<span class="price">¥{{ totalPrice.toFixed(2) }}</span>
          </span>
          <el-button type="primary" @click="handleCheckout">
            结算
          </el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()
const loading = ref(false)
const cartList = ref([])

// 计算总价
const totalPrice = computed(() => {
  return cartList.value.reduce((total, item) => {
    return total + item.price * item.quantity
  }, 0)
})

// 修改商品数量
const handleQuantityChange = (item) => {
  // TODO: 调用后端接口更新购物车商品数量
  console.log('更新数量:', item)
}

// 删除商品
const handleDelete = (item) => {
  ElMessageBox.confirm(
    '确认从购物车中删除该商品?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    // TODO: 调用后端接口删除购物车商品
    console.log('删除商品:', item)
    ElMessage.success('删除成功')
  }).catch(() => {})
}

// 继续购物
const handleContinueShopping = () => {
  router.push('/mall/product')
}

// 结算
const handleCheckout = () => {
  if (cartList.value.length === 0) {
    ElMessage.warning('购物车为空')
    return
  }
  // TODO: 跳转到结算页面
  router.push('/mall/order/create')
}
</script>

<style scoped>
.cart-footer {
  margin-top: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.total-price {
  margin-right: 20px;
  font-size: 16px;
}

.total-price .price {
  color: #f56c6c;
  font-size: 20px;
 